#include "DisplayManager.h"
#include "ObjectManager.h"
#include "InputManager.h"

#include "Game.h"
#include <stdio.h>

#include <stdio.h>
#include <stdlib.h>
#include "SDL/SDL_mixer.h"
#include <time.h>

//Stuff declared here due to use of a void function pointer (incompatible to Cpp)
void musicDone();
Mix_Music *music;


void CGame::musicHandleKey(char c) {
	switch(c) {
		case 'f':


      		/* Here we're going to have the 'f' key toggle the music on and
	 		off.  When it's on, it'll be loaded and 'music' will point to
	 		something valid.  If it's off, music will be NULL. */

      		if(music == NULL) {
	
				/* Actually loads up the music */
				music = Mix_LoadMUS("Art/Sounds/Gunshot.ogg");

				/* This begins playing the music - the first argument is a
	   			pointer to Mix_Music structure, and the second is how many
	   			times you want it to loop (use -1 for infinite, and 0 to
				have it just play once) */
				Mix_PlayMusic(music, 0);

				/* We want to know when our music has stopped playing so we
	   			can free it up and set 'music' back to NULL.  SDL_Mixer
	   			provides us with a callback routine we can use to do
				exactly that */
				Mix_HookMusicFinished(musicDone);
	
			} else {
				/* Stop the music from playing */
				Mix_HaltMusic();

				/* Unload the music from memory, since we don't need it
				anymore */
				Mix_FreeMusic(music);
	
				music = NULL;
			}
			break;

	}
}

/* This is the function that we told SDL_Mixer to call when the music
   was finished. In our case, we're going to simply unload the music
   as though the player wanted it stopped.  In other applications, a
   different music file might be loaded and played. */
void musicDone() {
	Mix_HaltMusic();
	Mix_FreeMusic(music);
	music = NULL;
}

CGame::CGame()
{
	m_omObjectManager = CObjectManager();
	m_dmDisplayManager = CDisplayManager();
	m_imInputManager = CInputManager();
	music = NULL;
	m_nTurn = 0;
}

CGame::~CGame()
{
	musicDone();           
}

///> Game event loop should be called here.
int CGame::run()
{
	bool quit = false;

	/* We're going to be requesting certain things from our audio
	device, so we set them up beforehand */
	int audio_rate = 22050;
	Uint16 audio_format = AUDIO_S16; /* 16-bit stereo */
	int audio_channels = 2;
	int audio_buffers = 4096;

	/* This is where we open up our audio device.  Mix_OpenAudio takes
	as its parameters the audio format we'd /like/ to have. */
	if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers)) {
		printf("Unable to open audio!\n");
		exit(1);
	}

	/* If we actually care about what we got, we can ask here.  In this
	program we don't, but I'm showing the function call here anyway
	in case we'd want to know later. */
	Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);

	/* We're going to be using a console to register keypresses
	 * in. 
	 */
	m_nTurn = 1;
	char event;
	bool increment = true;
	while(!quit)
	{
		//This processes input on the turn for game related issues
		quit = processTurn(event);
		endTurn(event);
		//this last line ends the turn cleanly.
    }
	Mix_CloseAudio();
	return 0;
}

bool CGame::processTurn(char &event)
{
	m_dmDisplayManager.drawMapPortion(m_omObjectManager);
	printf("Turn: %d   ", m_nTurn);
	event =m_imInputManager.getInput();
	char q;
	m_bIncrement = true;
	//Key handler (should probably move away from here)
	bool quit = false;
	switch(event)
    {
		case 'h':
		case '4':
			m_omObjectManager.movePlayer(-1,0);
			break;
		case 'l':
		case '6':
			m_omObjectManager.movePlayer(1,0);
			break;
		case 'k':
		case '8':
			m_omObjectManager.movePlayer(0,-1);
			break;
		case 'j':
		case '2':
			m_omObjectManager.movePlayer(0,1);
			break;
		case '1':
			m_omObjectManager.movePlayer(-1,1);
			break;
		case '3':
			m_omObjectManager.movePlayer(1,1);
			break;
		case '5':
			m_omObjectManager.movePlayer(0,0);
			break;
		case '7':
			m_omObjectManager.movePlayer(-1,-1);
			break;
		case '9':
			m_omObjectManager.movePlayer(1,-1);
			break;
		case 'Q':
			printf("Are you sure you want to quit!? (y/n) ");
			q = m_imInputManager.getInput();
			if((q == 'y') || (q == 'Y'))
				quit = true;
			else
			{
				m_bIncrement = false;
			}
			break;
		case 'f':
			break;
		default:
			m_bIncrement = false;
			break;
	}
	//turn = endTurn(event, turn, increment);
	return quit;     
}

int CGame::endTurn(char key)
{
	if(m_bIncrement)
		m_nTurn++;
	musicHandleKey(key);
	m_dmDisplayManager.clearScreen();
	return m_nTurn;
}
